home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / OOP_Course / Labs / Stack / Solution / Stack.m < prev    next >
Text File  |  1992-12-19  |  1KB  |  59 lines

  1. /*Objective_C Implementation of the Stack Class: Stack.m*/
  2.  
  3. //SOLUTION
  4.  
  5. #import <stdio.h>
  6. #import "Stack.h"
  7.  
  8. @implementation Stack
  9.  
  10. //Initialize a Stack instance of floating point elements
  11. -init
  12. {
  13.     return [self initCount:0 elementSize:sizeof(float) description:"f"];
  14.                                  //return id of newly created Stack instance
  15. }        
  16.                                                                
  17. //Add an element to the stack
  18. -push: (float)aNumber
  19. {
  20.     [self  addElement: &aNumber];    
  21.     return self;
  22. }
  23.  
  24. //Remove an element from the stack
  25. -(float)pop
  26. {
  27.     float number;
  28.     number = [self top];  //return value at top of stack
  29.                           //Empty stack returns zero
  30.     if (numElements != 0) 
  31.          [self removeLastElement];    /* This also automatically closes the gap */
  32.                                     /* at the end */
  33.     return number;
  34. }
  35.  
  36. -(float)top
  37. {
  38.     if (numElements == 0) return 0.0;
  39.     else return *(float *)[self elementAt:(numElements-1)];
  40. }
  41.  
  42. //Print the elements on the stack
  43. -printStack
  44. {
  45.     int i=0;
  46.     
  47.     if (numElements == 0) 
  48.         printf("Stack is empty.\n");
  49.     else {
  50.         printf("%i elements on stack, top to bottom:\n", numElements); 
  51.                     //numElements is an instance variable inherited from the Storage class
  52.         for(i=(numElements-1);i>=0;--i)
  53.             printf("    %f\n", *((float *)[self elementAt:i])); 
  54.      }
  55.     return self;
  56. }
  57.  
  58. @end
  59.